package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.res.ResMenuVo;
import com.hgxd.entity.vo.ReqRoleMenuVo;
import com.hgxd.service.IRoleMenuService;
import io.swagger.annotations.Api;
import org.apache.catalina.security.SecurityUtil;
import org.apache.el.parser.BooleanNode;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

import com.hgxd.result.Result;

import com.hgxd.service.SysMenuService;
import com.hgxd.entity.pojo.SysMenu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author fsd
 * @since 2023-10-18
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/api/sysMenu")
@CrossOrigin
public class SysMenuController {
    @Resource(name = "SysMenuServiceImpl")
    private SysMenuService sysMenuService;

    @Resource(name = "roleMenuServiceImpl")
    private IRoleMenuService roleMenuService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增菜单", notes = "传入菜单对象")
    @OperationLogAnnotation(operModule = "菜单管理-新增",operType = "新增",operContent = "新增目录，菜单或者按钮")
    public Result<Boolean> insert(@RequestBody SysMenu sysMenu) {
        return Result.ok(sysMenuService.save(sysMenu));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单", notes = "传入菜单对象（传入修改的即可，但是menuId必传）")
    @OperationLogAnnotation(operModule = "菜单管理-修改菜单",operType = "修改",operContent = "修改目录，菜单或者按钮")
    public Result<Boolean> update(@RequestBody SysMenu sysMenu) {
        return Result.ok(sysMenuService.updateById(sysMenu));
    }

    /**
     * 根据menuId删除
     */
    @PostMapping("/delete/{menuId}")
    @ApiOperation(value = "根据menuId删除", notes = "传入menuId")
    @OperationLogAnnotation(operModule = "菜单管理-根据id删除",operType = "删除",operContent = "根据menuId删除")
    public Result<Boolean> delete(@PathVariable("menuId") Long menuId) {
        return Result.ok(sysMenuService.removeById(menuId));
    }

    /**
     * 批量删除
     */
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除", notes = "传入数组")
    @OperationLogAnnotation(operModule = "菜单管理-批量删除",operType = "删除",operContent = "根据ids删除")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.ok(sysMenuService.removeByIds(ids));
    }

    /**
     * 查询全部目录
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部目录", notes = "无需传参")
    @OperationLogAnnotation(operModule = "菜单管理-查询目录",operType = "查询",operContent = "查询菜单目录")
    public Result<List<SysMenu>> findAll() {
        return Result.ok(sysMenuService.list(new QueryWrapper<SysMenu>().eq("type", 0)));
    }

    /**
     * 根据menuId查询子节点
     */
    @GetMapping("getById/{menuId}")
    @ApiOperation(value = "根据menuId查询子节点", notes = "传入menuId（用于懒加载）")
    @OperationLogAnnotation(operModule = "菜单管理-查询子菜单",operType = "查询",operContent = "查询子菜单")
    public Result<List<SysMenu>> findOne(@PathVariable("menuId") Long menuId) {
//        return Result.ok(sysMenuService.getById(menuId));
        return Result.ok(sysMenuService.list(new QueryWrapper<SysMenu>().eq("parent_id",menuId)));
    }


    /**
     * 根据userId返回树形结构
     * @return
     */
    @GetMapping("/getTreeByUserId/{userId}")
    @ApiOperation(value = "根据userId返回菜单树形结构", notes = "传入userId，获得对应用户权限所能看到的菜单")
    @OperationLogAnnotation(operModule = "菜单管理-查询菜单结构(树型)",operType = "查询",operContent = "查询菜单结构以树型结构展示")
    public Result<List<SysMenu>> getTreeByUserId(@PathVariable("userId") Long userId){
        List<SysMenu> list = sysMenuService.getTreeByUserId(userId);
        return Result.ok(list);
    }

    /**
     * 根据roleId查询menuIds
     * @param roleId
     * @return
     */

    @PostMapping("/getMenuByRoleId/{roleId}")
    @ApiOperation(value = "根据roleId查询menuIds", notes = "传入必备roleId")
    @OperationLogAnnotation(operModule = "根据roleId查询menuIds",operType = "查询",operContent = "根据roleId查询menuIds")
    public Result<List<ResMenuVo>> getMenuByRoleId(@PathVariable("roleId") Long roleId){
        return Result.ok(sysMenuService.getMenuByRoleId(roleId));
    }

    /**
     * 根据roleId修改menuId
     * @param reqRoleMenuVo
     * @return
     */
    @PostMapping("/updateMenuByRoleId")
    @ApiOperation(value = "根据roleId修改menuId",notes = "传入必备的roleId和对应修改的数组")
    @OperationLogAnnotation(operModule = "菜单管理-根据roleId修改menuId",operType = "修改",operContent = "根据roleId修改menuId")
    public Result<Boolean> updateMenuByRoleId(@RequestBody ReqRoleMenuVo reqRoleMenuVo){
        return Result.ok(roleMenuService.updateMenuByRoleId(reqRoleMenuVo));
    }
}

